﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace QLLopHoc.KetNoiDatabase
{
    public class DataAccess
    {
        //Chuoi ket noi co so du lieu Access voi ten
        //file la QLLopHoc.mbd ma ta da thiet ke
        protected static string _connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source='D:\php\nhap mon cong nghe phan mem\0971085\QLLopHoc\QLLopHoc\QLLopHoc.accdb'";
        static OleDbConnection connection;
        public static void OpenConnection()
        {
            try
            {
                if (connection == null)
                    connection = new OleDbConnection(_connectionString);
                connection.Open();
            }
            catch (Exception) 
            {
            }
        }
        public static void CloseConnection()
        {
            if (connection != null)
                connection.Close();
        }
        //phuong phap thuc thi 1 cau truy van SQL
        //gia tri tra ve la DataTable
        //phuong thuc nay dung ch cau lenh select
        public static DataTable ExecQuery(string sql)
        {
            OpenConnection();
            DataTable dt = new DataTable();
            OleDbCommand command = connection.CreateCommand();
            command.CommandText = sql;
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = command;
            adapter.Fill(dt);
            CloseConnection();
            return dt;
        }
       //phuong thuc thuc thi 1 cau truy van sql
       //gia tri tra ve la 1 so int (so record tac dong ) 
       //phuong thuc nay dung cho cau lenh update delete insert
        public static int ExecuteNonQuery(string sql)
        {
            OpenConnection();
            OleDbCommand command = connection.CreateCommand();
            command.CommandText = sql;
            int kq = command.ExecuteNonQuery();
            CloseConnection();
            return kq;
        }
        //Các phương thức truy vấn tự điều khiển đóng mở connection 
        public static DataTable ExecQueryManualConnect(string sql)
        {
            DataTable dt = new DataTable();
            OleDbCommand command = connection.CreateCommand();
            command.CommandText = sql;
            OleDbDataAdapter adapter = new OleDbDataAdapter();
            adapter.SelectCommand = command;
            adapter.Fill(dt);
            return dt;
        }

        public static int ExecNonQueryManualConnect(string sql)
        {
            OleDbCommand command = connection.CreateCommand();
            command.CommandText = sql;
            return command.ExecuteNonQuery();
        }

        //phương thức update database từ adapter 
        public static int ExecUpdate(string sql, DataTable dt)
        {
            OpenConnection();
            OleDbDataAdapter adapter = new OleDbDataAdapter(sql, connection);
            int kq = adapter.Update(dt);
            CloseConnection();
            return kq;
        }
    }
}
